#!/usr/bin/python
import cgi
import string
import os,sys
import traceback
from PageMaker import HTMLPage


def ValidateUser(log,pss):
	user_ok = 0
	f = open("user_info.txt","r")
	for line in f:
		tempInfo = line.strip()
		tempLine = tempInfo.split("|")
		if tempLine[0] == log and tempLine[1] == pss:
			user_ok = 1
	f.close()
	return user_ok

print "Content-type: text/html"
print

myPage = HTMLPage()

fs = cgi.FieldStorage()
user_auth = 1
user_name = fs.getvalue("log_id")
pswd = fs.getvalue("pswd")

try:
	ck_user = ValidateUser(user_name,pswd)
	if ck_user > 0:
		myPage.addTitle("Welcome to VITSystem")
		myPage.printMainPage(user_name)
	else:
		myPage.addMessageCode("Authentication Failed:red")
		myPage.addForm("login_action.cgi","post","goback")		
		myPage.addElement("<table align='center'>","","","")
		myPage.addElement("<tr><td colspan='2' style='font-weight: bold;' align='center'>VITS Login</td></tr>","","","")
		myPage.addElement("<tr><td colspan='1'>Login Id</td>","","","")
		myPage.addElement("<td colspan='1'>","text","log_id","")
		myPage.addElement("</td></tr><tr><td colspan='1'>Password</td><td colspan='1'>","password","pswd","")
		myPage.addElement("</td></tr><tr><td colspan='2' align='center'>","submit","save_log","Submit")
		myPage.addElement("</td></tr></table>","","","")

	myPage.printForm()
except:
	print "Unable to open file"
	etype = sys.exc_info()[0]
	evalue = sys.exc_info()[1]
	etb = traceback.extract_tb(sys.exc_info()[2])

	print "etype=",str(etype)
	print "evalue=",str(evalue)
	print "traceback",str(etb)

